package cn.edu.ctbu.firstdp.api;

import cn.edu.ctbu.firstdp.constant.REnum;
import cn.edu.ctbu.firstdp.entity.Student;
import cn.edu.ctbu.firstdp.exception.RException;
import cn.edu.ctbu.firstdp.service.StudentService;
import cn.edu.ctbu.firstdp.util.RUtil;
import cn.edu.ctbu.firstdp.vo.QueryObj;
import cn.edu.ctbu.firstdp.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/student")
public class StudentApiController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/list")
    public R<List<Student>> findAll(){
        List<Student> students = studentService.getALL();
        return RUtil.success(students);

    }
    @GetMapping("/{id}")
    public R<Student> findById(@PathVariable Integer id){
        Student student = studentService.findById(id);
        return RUtil.success(student);

    }
    @PostMapping("/add")
    public R<Student> add(Student student){

        return RUtil.success(studentService.add(student));
    }
    @PutMapping("/update1")
    public R<Student> update1(Student student){
        return RUtil.success(studentService.update1(student));
    }
//    @DeleteMapping("/delete1/{id}")
//    public R delete1(@PathVariable Integer id){
//        studentService.delete1(id);
//        return RUtil.success();
//    }
    @DeleteMapping("/delete2/{ids}")
    public R delete2(@PathVariable String ids) {
    // 将逗号分隔的字符串转换为 List<Integer>
        List<Integer> idList = Arrays.stream(ids.split(","))
                                        .map(Integer::valueOf)
                                        .collect(Collectors.toList());
    // 调用 Service 层的批量删除方法
    studentService.delete2(idList);
    // 返回成功响应
    return RUtil.success();
}
    @GetMapping("/validateUser")
    public R validateUsernameAndPassword(String sno,String password) throws Exception{
        return RUtil.success(studentService.validateUsernameAndPassword(sno, password));
    }

    @PostMapping("/getbypage/{num}")
    public R<Page< Student>> getByPage(@RequestBody QueryObj< Student> q0bj,@PathVariable Integer num){
        //按 id排倒序
        Sort sort;
        if(num==0)
        {
            sort = Sort.by(Sort.Direction.DESC,"id");
        }
        else {
            sort = Sort.by(Sort.Direction.ASC,"id");
        }

        Integer pageIndex=8;
        Integer pageSize=10;
        if(q0bj== null){
            //students为空就直接调用分页
            Pageable pageable = PageRequest. of(pageIndex,pageSize, sort);
            Page<Student> students = studentService.getALL(pageable);
            return RUtil.success(students.getContent(),students.getTotalElements());
        }
        else{
            pageIndex=q0bj.getPage()-1;
            pageSize=q0bj.getLimit();
            //判断是不是学生的一个实例
            if(q0bj.getData() instanceof Student){
                Student student = (Student) q0bj.getData();
                Pageable pageable = PageRequest.of(pageIndex,pageSize,sort);
                //按名字模糊查询，忽略空的
                ExampleMatcher matcher = ExampleMatcher.matching()
                        .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains()).withIgnoreNullValues();
                Example<Student> example = Example.of(student, matcher);
                Page<Student> studentPage = studentService.getALL(example, pageable);
                return RUtil.success(studentPage.getContent(),studentPage.getTotalElements());
            } else{
                throw new RException(REnum.QUERY_ERR);
            }
        }
    }
}
